#ifndef __CURTAIN_ACT_MANAGER__H__
#define __CURTAIN_ACT_MANAGER__H__


#include <mutex>
#include "json.h"
#include <string>
#include <vector>
#include <functional>
#include "Thread/Thread.h"
#include "Poller/Timer.h"
#include "StreamComposeMgr.h"
#include <set>


using namespace std;

namespace Media{
static const string configPathCurtain = "/mnt/mtd/Config/Curtain";
static const string defaultConfigPathCurtain = "/user/cfg/Curtain";
static const string paramsConfigPath = "/mnt/mtd/Config/Config11";
static const string asoundPath = "/proc/asound/cards";
static const string dymicCoordinatePath = "/mnt/mtd/Config/DymicCoordinate";

const int AirDefaultMeeting = 107;
const int RoomsDefaultMeeting = 106;
const int TailDefaultMeeting = 105;
const int RecordedBroadcastNum = 98;
const int DymicMirrorNum = 97;
const int MultiCameraSwitchNum = 96;
const int TailProductTest = 108;
const int RoomsProductTest = 109;
const int AirProductTest   = 110;

class CurtainActManager
{
public: 
	static CurtainActManager* instance();

	bool getDeviceCapacity(Json::Value &table);

	bool getStreamType(Json::Value &table);

	bool setPlayStream(const Json::Value &table);

	bool setCurtainLayout(const Json::Value &curtainTable, bool rwFlg = true);

	bool getCurtainLayout(Json::Value &table);
	
	bool playDefaultCurtain();

	bool setPlayStrategy(const Json::Value &table);

	bool SetDeviceShareStatus(const Json::Value &table);

	bool getCurtainPlayState(PlayState *state);

	bool getOutputInterface(Json::Value &table);

	bool playStartUpCurtain(const Json::Value table = Json::Value::null);

	uint8_t getUsbVideoInParam(void);

	bool isAudioNeedResampler(StreamInfo& inputStream, int inRate);

	bool getCurPlayNum(int* num);

	bool dymicSwitchCurtain(bool state);

	bool multiCameraSwitch(std::string cameraIp);

	bool multiCameraAddDel(std::string cameraIp);

	bool productTestCurtainPlay();

	bool isPlayDefCurtain(bool& iFlag, int& playNum);

	bool dymicSwitchTable(bool dymicFlg);

	bool uacoutChangeDeal(int playNum);

	bool cameraInfoUpdate(bool actVal, std::string devIp);

	bool getCameraInfo(std::set<std::string>& devIpSet);

	bool usbVideoRealTimeInfo(std::string &usbPort, bool &isH26x);

	bool isUsbVideoCapture();

	bool ctrlUsbVideo(bool actFlg);

	bool audioOutSwitch();

	bool usbStreamCheck(Json::Value& table);

	bool parseCurtainLayout(const Json::Value table);
	
public:
	CurtainActManager();
	~CurtainActManager();

	bool getStreamInfo(int streamType, StreamInfo* pStreamInfo, int *streamNum);

	bool getCurtainLayout(CurtainInfo* curtainInfo);

	bool getSpecificNumCurtain(Json::Value &table,  int curtainNum);

	bool getJsonTable(const string filePath, Json::Value &table);

	bool setJsonTable(const string filePath, Json::Value table);

	
private:
	bool initCurtianConfig();

	bool initCurtainActManager();

	bool readWriteCurtainJson(Json::Value &curtain, bool rwFlg);

	bool removeCurtainJson(int curtain_number);

    bool getFileNames(std::string path, std::vector<std::string>& filenames);

    bool typeToEnum(const std::string str, StreamInType* dst);

    bool resolutionToEnum(const std::string str, ResolutionDef* resn);
    
    bool parseCurtainStrategy(PlayStrategy strategy);

    bool dealPlayStream(int index, StreamPlayInfo playInfo, PlayStrategy strategy);

    bool outputToEnum(const std::string str, StreamOutInterface* type);

    bool dealOutInterface(StreamOutInterface type, PlayStrategy strategy);

    bool getOutputInfo(StreamOutInterface type, int *sampleRate, int *channels);

    bool getSoundNode(string mark, string &iNode);

    bool getDisplayinfo(const Json::Value table,std::string &info);

    bool saveCurPlayNum(int num);

	bool getDymicCoordinate(Json::Value &table);
	
	void detectThreadProc(Fdt::Thread *thread);

private:
    static CurtainActManager*			        sm_curtainActMgrIns;

	static std::mutex 			                sm_insMutex;

	Json::Value                                 m_supportStreamInfo;

	Json::Value                                 m_userConfigCurtain;

	std::map<int,StreamInfo>                    m_playVideoStreamMap;

	std::map<int,TaskInfo>                      m_playVideoTaskMap;

	std::map<int,StreamInfo>                    m_playAudioStreamMap;

	std::map<int,StreamPlayInfo>                m_playStreamMap;

    CurtainInfo                                 m_playCurtainInfo = {0};

    std::map<int,CurtainInfo>                   m_curtainInfoMap;

    PlayState                                   m_playState = IDLE;

    PlayStrategy                                m_curStrategy;

    std::mutex                                  m_streamMutex;

    std::mutex                                  m_curtainMutex;

    std::map<int, std::vector<StreamOutInterface> > m_interfaceVectorMap;

    int                                         m_playCurtainNum = 0;

    uint8_t                                     m_usbVideoInFlg = 0;

	std::string                                 m_lastSwitchCameraIp;

	std::map<std::string ,int>                  m_ipToChannelMap;

	std::mutex                                  m_multiCamreaMutex;

	std::mutex                                  m_cameraInfoMutex;

	std::set<std::string>                       m_cameraInfoSet;

	Json::Value 								m_recentSwitchTable;

	std::mutex                                  m_jsonFileMutex;

	int                                         m_dymicSetNum = 0;

	Json::Value 								m_playCurtainTable;

	StreamOutInterface                          m_currentAudioOut = HDMI_OUT;

	Fdt::Thread 						        m_detectThread{"CurtainActManagerThread1"};//thread放在最后,保证thread最后构造,最先析构
};

}//namespace Media

#endif


